<?php

namespace app\admin\validate;

use think\Validate;

class MenuRule extends Validate
{
    protected $failException = true;

    protected $rule = [
        'status' => 'checkStatus',
        'title'  => 'require',
        'name'   => 'require|unique:menu_rule',
        'weigh'  => 'number',
    ];

    /**
     * 验证提示信息
     * @var array
     */
    protected $message = [];

    /**
     * 字段描述
     */
    protected $field = [
    ];

    /**
     * 验证场景
     */
    protected $scene = [
        'add'        => ['status', 'title', 'weigh'],
        'edit'       => ['status', 'title', 'weigh'],
        'editStatus' => ['status']
    ];

    public function __construct()
    {
        $this->message = array_merge($this->message, [
            'title.require' => "规则标题不能为空",
            'name.require'  => "规则名称不能为空",
            'weigh.number'  => "权重只能为数字类型",
        ]);
        parent::__construct();
    }

    /**
     * 权限规则状态状态自定义验证规则
     * @param $value // 需验证的值
     * @param $rule // 验证规则
     * @param array $data // 数据
     */
    protected function checkStatus($value, $rule, array $data = [])
    {
        $status = (string)$data['status'] ?? ''; // 验证码
        if ($status === '') {
            return __('No rows updated');
        } else if ($status !== '0' && $status !== '1') {
            return __('No rows updated');
        }
        return true;
    }

}
